home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5437 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: erich.triumf.ca!bennett
  2. From: bennett@erich.triumf.ca (P.Bennett)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Question about sscanf
  5. Date: 8 Feb 1996 22:10 PST
  6. Organization: TRIUMF: Tri-University Meson Facility
  7. Distribution: world
  8. Message-ID: <8FEB199622105557@erich.triumf.ca>
  9. References: <4fe1oq$kuf@zippy.cais.net>
  10. NNTP-Posting-Host: erich.triumf.ca
  11. News-Software: VAX/VMS VNEWS 1.50    
  12.  
  13. In article <4fe1oq$kuf@zippy.cais.net>, usaid@cais.cais.com (USAID) writes...
  14. >Here is a fragment of code that I'm having a problem with.  The program 
  15. >is bombing on the line containing the call to sscanf.  It assigns to the 
  16. >first variable fine, but blows up on the second one.  I have no idea 
  17. >what's wrong.
  18. >int formatFile(void)
  19. >{
  20. ....
  21. >   char *name; 
  22. >   char *value; 
  23. ...
  24. >      /* store the name and value in variables */
  25. >      if (sscanf(line, "%s %s", name, value) != 2)
  26.  
  27. name and value are uninitialized pointers - they may be pointing anywhere, so
  28. sscanf() will write the name and value strings in some random location.
  29.  
  30. You must _never_ use a pointer without first making sure it points to a
  31. suitable memory space.
  32.  
  33. Either make name and value arrays of suitable size, thus:
  34.     char name[80];
  35.     char value[20];  /* or whatever size is suitable...  */
  36. or use malloc() to allocate space for them to point to.
  37.  
  38. Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight
  39. Internet: bennett@triumf.ca         | of one another only when one can be
  40. Packet: ve7cei@ve7kit.#vanc.bc.ca   | observed visually from the other
  41. TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
  42. GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
  43.  
  44.